<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // function SortArr(arr) {
    //   for(i = 0; i < arr.length; i++) {
    //     for(j = i+1; j < arr.length; j++) {
    //       if(arr[i] > arr[j]) {
    //         let tem = null
    //         tem = arr[i]
    //         arr[i] = arr[j]
    //         arr[j] = tem
    //       }
    //     }
    //   }
    //   return arr
    // }
    function SortArr(arr) {
      if(arr.length <= 1) return arr
      let index = Math.floor(arr.length/2)
      let value = arr.splice(index,1)
      // console.log(middle);
      let left = []
      let right = []
      for(let i = 0; i < arr.length; i++) {
        if(arr[i] < value) {
          left.push(arr[i])
        }else {
          right.push(arr[i])
        }
      }
      // console.log(arr);
      return SortArr(left).concat(value, SortArr(right))
    }
    console.log(SortArr([3,5,6,2,1]));
    // console.log(SortArr([5,7,2,9,3]));
  </script>
</body>
</html>